package com.flexcloudsdk.taskqueue;
import java.io.UnsupportedEncodingException;

import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Logger;
import com.google.appengine.api.taskqueue.TaskHandle;

public class CloudTaskHandle {
	private static final Logger log = Logger.getLogger(CloudTaskHandle.class.getName());
	private TaskHandle taskHandle = null;
	private String sourceMethod;
	private String sourceClass;


	public CloudTaskHandle() {
		log.info("CloudTaskHandle has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudTaskHandle.class.getName();
	}

	public CloudTaskHandle(TaskHandle handle) {
		sourceClass = CloudTaskHandle.class.getName();
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.entering(sourceClass, sourceMethod);
		
		taskHandle = handle;
		
		log.exiting(sourceClass, sourceMethod);
	}
	
	public TaskHandle getHandle() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskHandle;
	}
	
	public List<Entry<String,String>>   extractsParams() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		List<Entry<String,String>> value = null;
		try {
			value =  taskHandle.extractParams();
		} catch(UnsupportedEncodingException exception) {
			log.severe(exception.getMessage());
		}

		log.exiting(sourceClass, sourceMethod);
		return value;
	}
	
	public long getEtaMillis() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskHandle.getEtaMillis();
	}

	public String getName() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskHandle.getName();
	}
	
	public byte[] getPayload() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskHandle.getPayload();
	}
	
	public String getQueueName() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return taskHandle.getQueueName();
	}
	
	public Integer getRetryCount() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.entering(sourceClass, sourceMethod);

		return taskHandle.getRetryCount(); 
	}
	
	@Override
	public int hashCode() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.entering(sourceClass, sourceMethod);

		return taskHandle.hashCode();
	}
	
	@Override
	public String toString() {
		sourceMethod = Thread.currentThread().getClass().getEnclosingMethod().getName();
		log.entering(sourceClass, sourceMethod);

		return taskHandle.toString();
	}
}	
